# 导入request
import requests
# 发送请求
# response = requests.请求方法(url="URL地址", params={k:v}, headers={k:v},
#                          data={k:v}, json={k:v}, cookies="cookie数据")
# 3. 获取结果
# reponse.响应数据
# CRMEB
# url：http://8.140.193.160:8899/admin/home/
# 账号admin   920509
# 登录，发起请求：POST
base_url = "http://8.140.193.160:8899"

response = requests.post(url = base_url + "/adminapi/login",
                         json ={"account":"admin","pwd":"920509","imgcode":"a"} )

# 登录，接收响应
# print(response.json()) # json()  返回的格式 是python的原始对象：字典
# 根据响应结果，提取response 内容
# 提取token
"""
print(response.json()['data']['token'])
# 提取status
print(response.json()['status'])
# 提取第一个path
print(response.json()['data']['menus'][0]['path']) # /admin/home/
# 提取title：产品添加，前提，每一次的列表都会变，不知道这个title具体位置
for i in response.json()['data']['menus']:
    if i['title'] == "产品添加":
        print(i['title']) #　/admin/home/
"""
# response的返回
"""
print(response.url) # 返回请求地址：http://8.140.193.160:8899/adminapi/login
# print(response.content) # 返回响应内容,二进制
# print(response.text) # 返回响应内容 ，字符串
# print(response.json()) # 返回响应内容，字典形式返回
print(response.status_code) # 返回响应状态码
print(response.headers) # 返回响应头
print(response.elapsed.microseconds) # 获取响应时间
print(response.encoding) # 获取响应头字符编码
print(response.cookies) # 获取Cookie
"""
#
# 关联token参数
# 提取token
token = response.json()['data']['token']

# 客服话术查询：GET ，将参数拼接到URL的后面
# 将取到的token传入接口里
# res = requests.get(base_url+"/adminapi/app/wechat/speechcraft?page=1&limit=15&cate_id=",headers={"Authori-zation":
#                                                                                                "Bearer " + str(token)})
# 客服话术查询： GET，将参数写进params
"""
res = requests.get(base_url+"/adminapi/app/wechat/speechcraft",params={"page":"1","limit":15},headers={"Authori-zation":
                                                                                               "Bearer " + str(token)})
print(res.json())
# 添加断言，看是否通过
assert res.status_code == 200 # 断言响应状态码是否是200
assert res.json()['msg'] == 'ok'
"""
# 不二掌柜登录： form表单 Authorization: Basic YnVlcnl1bl9wYzpidWVyeXVuX3Bj
"""
response2 = requests.post("https://jxc.tao51d.com/api/oauth/token?grant_type=password",data={"username":"13066761104","password":"qwe123.."},
                          headers={"Authorization":"Basic YnVlcnl1bl9wYzpidWVyeXVuX3Bj"})
print(response2.json())
"""

# 使用requests上传图片
"""
url = "https://graph.baidu.com/upload" # https://graph.baidu.com/upload
files = {
    "image":("333.jpg",open('C:\\Users\\Administrator\\Desktop\\333.jpg','rb'),"image/jpeg"),
}
r = requests.post(url,files=files)
print(r.json())
"""